package modelo.compra;

import java.util.ArrayList;

import modelo.compra.Compra;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class CompraDAO implements CompraDAOInterface{

	private static SessionFactory factory; 

	public CompraDAO() {
		try{
			factory = new AnnotationConfiguration().
					configure().
					//addPackage("com.xyz") //add package if used.
					addAnnotatedClass(Compra.class).
					buildSessionFactory();
		}catch (Throwable ex) { 
			System.err.println("Falha ao criar o objeto SessionFactory." + ex);
			throw new ExceptionInInitializerError(ex); 
		}  

	}

	@Override
	public Compra getById(int id) {
		Session session = factory.openSession();
		Transaction tx = null;
		Compra compra = null;
		try{
			tx = session.beginTransaction();
			compra = (Compra) session.get(Compra.class, id);
			tx.commit();
		}catch (HibernateException e) {
			if (tx!=null) tx.rollback();
			e.printStackTrace(); 
		}finally {
			session.close(); 
		}

		return compra;
	}

	@Override
	public ArrayList<Compra> getListAll() {
		Session session = factory.openSession();
		Transaction tx = null;
		ArrayList<Compra> compras = null;
		try{
			tx = session.beginTransaction();
			compras = (ArrayList<Compra>) session.createQuery("FROM Compra").list(); 
			tx.commit();
		}catch (HibernateException e) {
			if (tx!=null) tx.rollback();
			e.printStackTrace(); 
		}finally {
			session.close(); 
		}
		return compras;

	}

	@Override
	public ArrayList<Compra> getList(String condicao) {
		Session session = factory.openSession();
		Transaction tx = null;
		ArrayList<Compra> compras = null;
		try{
			tx = session.beginTransaction();
			compras = (ArrayList<Compra>) session.createQuery("FROM Compra " + condicao).list(); 
			tx.commit();
		}catch (HibernateException e) {
			if (tx!=null) tx.rollback();
			e.printStackTrace(); 
		}finally {
			session.close(); 
		}
		return compras;
	}

	@Override
	public void insert(Compra compra) {
		Session session = factory.openSession();
		Transaction tx = null;

		try{

			tx = session.beginTransaction();
			session.save(compra);
			tx.commit();

		}catch (HibernateException e) {

			if (tx!=null) tx.rollback();
			e.printStackTrace(); 

		}finally {

			session.close(); 

		}

	}

	@Override
	public void delete(int id) {
		Session session = factory.openSession();
		Transaction tx = null;

		try{

			tx = session.beginTransaction();
			session.delete(getById(id));
			tx.commit();

		}catch (HibernateException e) {

			if (tx!=null) tx.rollback();
			e.printStackTrace(); 

		}finally {

			session.close(); 

		}
	}

	@Override
	public void update(Compra compra) {
		Session session = factory.openSession();
		Transaction tx = null;
		try{
			tx = session.beginTransaction(); 
			session.update(compra); 

			tx.commit();
		}catch (HibernateException e) {
			if (tx!=null) tx.rollback();
			e.printStackTrace(); 
		}finally {
			session.close(); 
		}
	}

}
